import requests
import pandas as pd
from pathlib import Path
import re
import time

# 创建目标文件夹
download_folder = Path('美食视频')
download_folder.mkdir(parents=True, exist_ok=True)

# 从CSV文件中读取数据
df = pd.read_csv(f'好看视频.csv')

# 筛选播放量不低于10000的视频
data = df.query('播放量 >= 10000')
data = data.sort_values(by='播放量', ascending=False)

# 设置请求头信息
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'}

# 遍历读取的数据
for r in range(data.shape[0]):
    # 从数据中选取一行并转换成字典
    row = data.iloc[r].to_dict()
    # 提取“标题”和“网址”
    video_title = row['标题']
    video_title = re.sub(r'[\\/:*?"<>|]', '', video_title)
    video_url = row['网址']
    # 构造视频保存路径
    file_name = f'{r + 1}.{video_title}.mp4'
    file_path = download_folder / file_name
    # 跳过已存在的视频
    if file_path.exists():
        print(f'[{file_name}]已存在，跳过')
        continue
    try:
        # 对视频网址发起请求并启用分块下载
        with requests.get(url=video_url, stream=True, headers=headers) as response:
            # 逐块读取数据并写入本地文件
            with open(file=file_path, mode='wb') as f:
                chunk_size = 1024 * 1024  # 设置缓冲区大小为1MB
                for chunk in response.iter_content(chunk_size=chunk_size):
                    if chunk:
                        f.write(chunk)
                print(f'[{file_name}]下载完成')
    except Exception as e:
        print(f'[{file_name}]下载失败')
    # 适当暂停，以免触发反爬
    time.sleep(5)
